const db = {
  users: [{ name: 'tobi' }, { name: 'loki' }, { name: 'jane' }]
};

module.exports = function users(req, res, next) {
  const match = req.url.match(/^\/user\/(.+)/);
  if (match) {
    const user = db.users[match[1]];
    if (user) {
      res.setHeader('Content-type', 'application/json');
      res.end(JSON.stringify(user));
    } else {
      const err = new Error('User not found');
      err.noFound = true;
      next(err);
    }
  } else {
    next();
  }
};
